package com.xjj.service.mapper;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xjj.entity.Leave;
import com.xjj.vo.LeaveVO;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author : 王泰元
 * @date : 2022/10/24 17:16
 */
@Repository
public interface LeaveMapper extends BaseMapper<Leave> {

    @SelectProvider(type = LeaveSqlProvider.class, method = "select")
    List<Leave> getByCondition(LeaveVO leaveVo);

    @Select("select count(0) " +
            "from xjj_klass_teacher " +
            "where manager_id = #{managerId} " +
            "and klass_id = #{klassId}")
    Integer isYourTeacher(@Param("managerId") Integer managerId, @Param("klassId") Integer klassId);

    /* ***********************************/
    /*        sql 语句构建器               */
    /* ***********************************/

    /**
     * sql 语句构建器；
     * 根据传入的 LeaveVO（封装了条件）、method，产生并返回对应的动态 sql 语句
     */
    @Slf4j
    class LeaveSqlProvider {

        /**
         * 得到动态产生的多条件查询 sql 语句
         *
         * @param leaveVO 封装了查询条件的 Leave 实例
         * @return 动态产生的 sql 查询语句
         */
        public String select(LeaveVO leaveVO) {
            Leave leave = leaveVO.getLeave();
            return new SQL() {
                {
                    SELECT("l.*, s.name as studentName, k.name as klassName, p.name as parentName")
                            .FROM("xjj_leave l ")
                            .LEFT_OUTER_JOIN("xjj_student s ON l.student_id = s.id ",
                                    "xjj_klass k ON l.klass_id = k.id ",
                                    " xjj_parent p ON s.parent_id = p.id ");
                    if (StrUtil.isNotBlank(leave.getType())) {
                        WHERE("l.type = #{leave.type} ");
                    }
                    if (StrUtil.isNotBlank(leave.getStatus())) {
                        WHERE("l.status = #{leave.status} ");
                    }
                    if (leave.getStudentId() != null && leave.getStudentId() != 0) {
                        WHERE("l.student_id = #{leave.studentId} ");
                    }
                    if (leave.getKlassId() != null && leave.getKlassId() != 0) {
                        WHERE("l.klass_id = #{leave.klassId} ");
                    }
                    if (StrUtil.isNotBlank(leaveVO.getStartSendTime())) {
                        WHERE("l.send_time >= #{startSendTime} ");
                    }
                    if (StrUtil.isNotBlank(leaveVO.getEndSendTime())) {
                        WHERE("l.send_time <= #{endSendTime} ");
                    }
                    if (leave.getKlassId() == null && leaveVO.getKids() != null && !leaveVO.getKids().isEmpty()) {
                        StringBuilder s = new StringBuilder("");
                        for (Integer kid : leaveVO.getKids()) {
                            s.append(kid).append(",");
                        }
                        s.deleteCharAt(s.length() - 1);

                        WHERE("l.klass_id in (" + s + ") ");
                    }
                    if (leaveVO.getLeave().getStudentId() == null && leaveVO.getSids() != null && !leaveVO.getSids().isEmpty()) {
                        StringBuilder s = new StringBuilder("");
                        for (Integer sid : leaveVO.getSids()) {
                            s.append(sid).append(",");
                        }
                        s.deleteCharAt(s.length() - 1);

                        WHERE("l.student_id in (" + s + ") ");
                    }
                    ORDER_BY("send_time DESC");
//                    LIMIT(leaveVO.getPageSize()).OFFSET((long) (leaveVO.getPageNum() - 1) * leaveVO.getPageSize());
                }
            }.toString();
        }
    }
}
